package com.example.questionnaire.common;

import com.example.questionnaire.shiro.CORSAuthenticationFilter;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * 配置安全框架：Shiro
 */

@Configuration
public class ShiroConfig {

    // remeberMe cookie 加密的密钥 各个项目不一样 默认AES算法 密钥长度（128 256 512）
    private static final String ENCRYPTION_KEY = "3AvVhmFLUs0KTA3Kprsdag==";





    //配置Shiro注解的支持,spring aop的思想
    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    @DependsOn({"lifecycleBeanPostProcessor"})
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }


   //配置密码匹配规则
   @Bean("hashedCredentialsMatcher")
   public HashedCredentialsMatcher hashedCredentialsMatcher(){
       HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
       //配置加密方式：MD5
       hashedCredentialsMatcher.setHashAlgorithmName("MD5");
       //加密次数：100
       hashedCredentialsMatcher.setHashIterations(100);
       //因为加密后的字符串为16进制，这里要把这个参数改为true
       hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
       return hashedCredentialsMatcher;
   }


    @Bean
    public CORSAuthenticationFilter corsAuthenticationFilter(){
        return new CORSAuthenticationFilter();
    }

//    @Bean
//    public SimpleCookie rememMeCookie() {
//        // 初始化设置cookie的名称
//        SimpleCookie simpleCookie = new SimpleCookie("remember-me");
//        simpleCookie.setPath("/#/");
//        simpleCookie.setMaxAge(86400);// 设置cookie的生效时间
//        simpleCookie.setHttpOnly(true);
//        return simpleCookie;
//    }
//
//    @Bean
//    public CookieRememberMeManager rememberMeManager() {
//        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
//        cookieRememberMeManager.setCookie(rememMeCookie());
//        // remeberMe cookie 加密的密钥 各个项目不一样 默认AES算法 密钥长度（128 256 512）
//        cookieRememberMeManager.setCipherKey(Base64.decode(ENCRYPTION_KEY));
//        return cookieRememberMeManager;
//    }

}

